angular.module('utilsModule', []).factory('utilsService', function($http) {
    return {
        isEmptyObject: function(obj) {
            for (var t in obj)
                return !1;
            return !0;
        },
        GetRequestParams: function() {
            var url = location.search;
            var theRequest = {};

            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
                }
            }
            //console.log(this);
            if (!this.isEmptyObject(theRequest)) {
                return theRequest;
            } else {
                return false;
            }
        },
        reqParamSerialize: function(obj) {
            var query = '',
                name, value, fullSubName, subName, subValue, innerObj, i;
            for (name in obj) {
                value = obj[name];
                if (value instanceof Array) {
                    for (i = 0; i < value.length; ++i) {
                        subValue = value[i];
                        fullSubName = name + '[' + i + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value instanceof Object) {
                    for (subName in value) {
                        subValue = value[subName];
                        fullSubName = name + '[' + subName + ']';
                        innerObj = {};
                        innerObj[fullSubName] = subValue;
                        query += param(innerObj) + '&';
                    }
                } else if (value !== undefined && value !== null)
                    query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
            }
            return query.length ? query.substr(0, query.length - 1) : query;
        },
        getLastMonthDay: function(year, month) {
            //var firstdate = year + '-' + month + '-01';
            var day = new Date(year, month, 0);
            return year + '-' + month + '-' + day.getDate();

        },
        httpRequest: function(url, method, data) {
            return $http({
                url: url,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                method: method,
                data: data
            });
        },
        getDefaultRequestData: function() {
            if (this.GetRequestParams()) {
                return this.reqParamSerialize(this.GetRequestParams());
            } else {
                return this.reqParamSerialize({ date: this.getLastMonthDay(new Date().getFullYear(), (new Date().getMonth() + 1) < 10 ? "0" + (new Date().getMonth() + 1) : (new Date().getMonth() + 1)) });
            }
        },
        getCurrentTimestamp: function() {
            //如: 1501837534000, 返回毫秒，如果反转为年月日时分秒需要先除1000
            return Date.parse(new Date());
        }
    };
});